"use strict";
require("test").setup();

var Coroutine = require('coroutine');
var hash = require('hash');

var str_prefix = "HYS";

describe("meetingroom", function() {
    before(function() {
        httpPost("GET", "/v1.0/api/user/Sync", {})
        // 用户登录
        var t = "123"
        var result = httpPost("POST", "/v1.0/api/user/WebLogin", {
            "phone": "18888888888",
            "password": hash.md5(hash.md5("ZHYQ2020").digest().hex() + t).digest().hex(),
            "t": t,
        })
    });

    after(function() {
        clearDB();
    });

    it("创建会议室", function() {
        var result = httpPost("POST", "/v1.0/api/meetingroom/Create", {
            "name": "会议室101",
            "load": 20,
            "building": "中心楼",
            "floor": 1,
            "device": "白板,空调,会议桌椅",
            "remark": "隔音不好",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);
    });

    it("编辑会议室", function() {
        var result = httpPost("POST", "/v1.0/api/meetingroom/Update", {
            "id": 1,
            "name": "会议室102",
            "device": "白板,空调",
        })
        assert.equal(result.code, "200");
    });

    it("查看会议室列表-名称查询", function() {
        var result = httpPost("POST", "/v1.0/api/meetingroom/List", {
            "name": "10",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list[0].id, 1);
        assert.equal(result.data.list[0].no, str_prefix + "001");
        assert.equal(result.data.list[0].name, "会议室102");
        assert.equal(result.data.list[0].load, 20);
        assert.equal(result.data.list[0].building, "中心楼");
        assert.equal(result.data.list[0].floor, 1);
        assert.equal(result.data.list[0].device[0].enu_value, "白板");
        assert.equal(result.data.list[0].device[1].enu_value, "空调");
        assert.equal(result.data.list[0].remark, "隔音不好");
        assert.equal(result.data.total, 1);
    });

    it("会议室列表-统计", function() {
        var result = httpPost("POST", "/v1.0/api/meetingroom/Static", {
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.total, 1);
        assert.equal(result.data.disable, 0);
        assert.equal(result.data.occupy, 0);
        assert.equal(result.data.free, 1);
    });

    it("查看会议室详情", function() {
        var result = httpPost("POST", "/v1.0/api/meetingroom/Get", {
            "id": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);
        assert.equal(result.data.no, str_prefix + "001");
    });

    it("停用-启用 会议室", function() {
        var result = httpPost("POST", "/v1.0/api/meetingroom/Update", {
            "id": 1,
            "status": "已停用",
        })
        assert.equal(result.code, "200");

        result = httpPost("POST", "/v1.0/api/meetingroom/Update", {
            "id": 1,
            "status": "启用中",
        })
        assert.equal(result.code, "200");
    });

    it("删除会议室", function() {
        var result = httpPost("POST", "/v1.0/api/meetingroom/Del", {
            "id": 1,
        })
        assert.equal(result.code, "200");

        result = httpPost("POST", "/v1.0/api/meetingroom/List", {
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 0);
    });

    it("有未完成的预约 不影响停用会议室 -> 停用后，预约产生告警消息", function() {
        var result = httpPost("POST", "/v1.0/api/meetingroom/Create", {
            "name": "会议室201",
            "load": 20,
            "building": "中心楼",
            "floor": 2,
            "device": "白板,空调,会议桌椅",
            "remark": "隔音不好",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 2);

        result = httpPost("POST", "/v1.0/api/meetingRecord/Create", {
            "meetingroom_id": 2,
            "start_date_time": "2099-12-15 10:00",
            "end_date_time": "2099-12-15 12:00",
            "meeting_title": "例行周会",
            "meeting_content": "各抒己见",
            "user_number": 10,
            "reviewed_by": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);

        result = httpPost("POST", "/v1.0/api/meetingroom/Update", {
            "id": 2,
            "status": "已停用",
        })
        assert.equal(result.code, "200");

        result = httpPost("POST", "/v1.0/api/meetingRecord/Get", {
            "id": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);

        result = httpPost("POST", "/v1.0/api/message/List", {})
        assert.equal(result.code, "200");
        assert.equal(result.data.list[0].id, 1);
        assert.equal(result.data.list[0].content, "您申请的会议室已停用，请及时处理");
    });

    it("有预约 不允许删除会议室", function() {
        var result = httpPost("POST", "/v1.0/api/meetingroom/Del", {
            "id": 2,
        })
        assert.equal(result.code, "400");
        assert.equal(result.msg, "会议室已有预约业务,不允许删除会议室");
    });

    it("查看会议室列表-状态查询", function() {
        var result = httpPost("POST", "/v1.0/api/meetingroom/List", {
            "status": "已停用",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list[0].id, 2);
        assert.equal(result.data.list[0].no, "HYS001");
        assert.equal(result.data.list[0].name, "会议室201");
        assert.equal(result.data.list[0].floor, 2);
        assert.equal(result.data.total, 1);
    });
});

require('test').run();